package services;

import tw.edu.tku.im.maze.AddRoomDeliverer;
import tw.edu.tku.im.maze.AddRoomRep;
import tw.edu.tku.im.maze.AddRoomReq;
import tw.edu.tku.im.maze.DeliveryListenerI;
import tw.edu.tku.im.maze.RepPacket;
import tw.edu.tku.im.maze.TimeOutRep;
import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

public class CreatRoomService extends IntentService 
                              implements DeliveryListenerI{
   //[ -------------------- constant --------------------
   public static final String BROADCAST_ACTION="CREAT_ROOM_REP",
                              STATUS="status",
                              PACKET="packet";
   //] -------------------- constant --------------------
   
   //[ -------------------- construct --------------------
   public CreatRoomService()            { super("CreatRoomService"); }
   public CreatRoomService(String name) { super(name); }
   //] -------------------- construct --------------------
   
   // -------------------------------------------------- 
   @Override
   protected void onHandleIntent(Intent intent) {
//android.util.Log.d("DebugMsg", "CreatRoomService catch!");      
      try {
         AddRoomReq request= new AddRoomReq(intent.getStringExtra("roomName"),
                                            intent.getStringExtra("userId"),
                                            intent.getIntExtra("ticket",-1),
                                            intent.getIntExtra("mapLv",-1),
                                            intent.getIntExtra("mode", -1),
                                            intent.getStringExtra("roomPwd"));       
         AddRoomDeliverer mAddRoomDeliverer= new AddRoomDeliverer();
//android.util.Log.d("DebugMsg", "CreatRoomService req has sended!");          
         mAddRoomDeliverer.deliver(request, this);
      } catch(Exception e) {}      
   }
   // --------------------------------------------------
   @Override
   public void processReply(RepPacket rep) {
//android.util.Log.d("DebugMsg", "CreatRoomService rep back!");      
      Intent localIntent= new Intent(BROADCAST_ACTION);      
      localIntent.putExtra(PACKET, rep);
//android.util.Log.d("DebugMsg", "CreatRoomService broadcast intent!");      
      LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
   }
   // --------------------------------------------------
}
